package gsw.tool.skin;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

import gsw.tool.skin.utils.SkinThemeUtils;

/**
 * Created by siwei on 2018/3/18.
 */

/**
 * 布局加载器
 * 用来设置view的属性
 */
class SkinLayoutFactory implements LayoutInflater.Factory2, Observer {
    private static final String[] mClassPrefixList = {
            "android.widget.",
            "android.view.",
            "android.webkit."
    };

    private static final Class<?>[] mConstructorSignature = new Class[]{Context.class, AttributeSet.class};
    private static final HashMap<String, Constructor<? extends View>> mConstructorMap = new HashMap<>();
    SkinAttribute skinAttribute;//属性处理类
    Activity activity;
    Typeface typeface;

    public SkinLayoutFactory() {
        this(null);
    }

    public SkinLayoutFactory(Activity activity) {
        typeface = SkinThemeUtils.getSkinTypeface(activity);
        skinAttribute = new SkinAttribute(typeface);
        this.activity = activity;
    }

    @Override
    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
        View view = createViewFromTag(name, context, attrs);
        if (view == null) {
            view = createView(name, context, attrs);
        }
        //筛选需要换肤的View
        skinAttribute.load(view, attrs);
        return view;
    }

    /**
     * 创建系统View
     *
     * @param name
     * @param context
     * @param attrs
     * @return
     */
    private View createViewFromTag(String name, Context context, AttributeSet attrs) {
        View view = null;
        //如果是自定义view就返回null
        if (-1 != name.indexOf(".")) {
            return view;
        }
        for (int i = 0; i < mClassPrefixList.length; i++) {
            view = createView(mClassPrefixList[i] + name, context, attrs);
            if (view != null) {
                break;
            }
        }
        return view;
    }

    /**
     * 通过反射获取构造函数来创建view，同时缓存构造函数
     *
     * @param name
     * @param context
     * @param attrs
     * @return
     */
    private View createView(String name, Context context, AttributeSet attrs) {
        Constructor<? extends View> constructor = mConstructorMap.get(name);
        if (constructor == null) {
            try {
                Class<? extends View> mclass = context.getClassLoader().loadClass(name).asSubclass(View.class);
                constructor = mclass.getConstructor(mConstructorSignature);
                mConstructorMap.put(name, constructor);
            } catch (Exception e) {
            }
        }
        if (constructor != null) {
            try {
                return constructor.newInstance(context, attrs);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return null;
    }

    @Override
    public void update(Observable o, Object arg) {
        SkinThemeUtils.updateStatusBar(activity);
        Typeface typeface = SkinThemeUtils.getSkinTypeface(activity);
        skinAttribute.setTypeface(typeface);
        //换皮肤
        skinAttribute.applySkin();
    }
}
